/**
 * 文 件 名:  TimeServerHandler.java
 * 工 程 名:  Mina
 * 创建日期:  2015年4月8日 下午3:02:58
 * 创建作者:  杨 强  <281455776@qq.com>
 */

import java.util.Date;
import java.util.Iterator;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class TimeServerHandler extends IoHandlerAdapter {
    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        cause.printStackTrace();
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        String str = message.toString();
        if (str.trim().equalsIgnoreCase("quit")) {
            session.close();
            return;
        }
        System.out.println(session.getLocalAddress().toString());
        System.out.println(session.getRemoteAddress().toString());
        System.out.println(session.getServiceAddress().toString());
        Iterator<Object> it = session.getAttributeKeys().iterator();
        while (it.hasNext()) {
            System.out.println(session.getAttribute(it.next()));
        }
        Date date = new Date();
        session.write(date.toString());
        System.out.println("Message written...");
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        System.out.println("IDLE " + session.getIdleCount(status));
    }
}
